package com.sicheng.datastruct.queue;

@SuppressWarnings("unused")
public class ArrayQueue {
    private int MAX_SIZE;//队列最大容量
    private int front;  //头指针，指向队列首元素的前一个位置
    private int rear;   //尾指针,指向队列最后元素
    private int[] queue;//模拟队列的数组

    public ArrayQueue() {
    }

    public ArrayQueue(int MAX_SIZE) {
        this.MAX_SIZE = MAX_SIZE;
        this.front = -1;
        this.rear = -1;
        this.queue = new int[MAX_SIZE];
    }

    /**
     * 判断队列是否为空
     *
     * @return y|n
     */
    public boolean isEmpty() {
        return rear == front;
    }

    /**
     * 队列是否装满
     *
     * @return y|n
     */
    public boolean isFull() {
        return rear == MAX_SIZE - 1;
    }


    /**
     * 数据入队
     *
     * @param e 入队元素
     */
    public void add(int e) {
        if (isFull()) {
            return;
        }
        rear++;
        queue[rear] = e;
    }

    /**
     * 出队
     *
     * @return 出队元素
     */
    public int get() {
        if (isEmpty()) {
            throw new RuntimeException("队列空，不能取数据");
        }
        front++;
        return queue[front];
    }

    public void catAll() {
        if (isEmpty()) {
            System.out.println("队列为空");
            return;
        }
        int front, rear;
        front = this.front;
        rear = this.rear;
        for (int i = front + 1; i <= rear; i++) {
            System.out.printf("queue[%d]=%d%n",i,queue[i]);
        }
    }


    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return queue[front + 1];
    }

}
